package com.util;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.imageio.ImageIO;

import com.swetake.util.Qrcode;

public class QrcodePic {

	public void qrCode(String str, Integer x, Integer y, Integer s) throws UnsupportedEncodingException{
		Qrcode rcode = new Qrcode();
		rcode.setQrcodeVersion(3);
		byte[] content = str.getBytes("utf-8");
		BufferedImage bufImg = new BufferedImage(300, 300,
					        BufferedImage.TYPE_INT_RGB);
		
		Graphics2D gs = bufImg.createGraphics(); 
		gs.setBackground(Color.WHITE);
		gs.clearRect(0, 0, 300, 300); 
		gs.setColor(Color.BLACK); 
		if (content.length > 0 && content.length < 120) {
            boolean[][] codeOut = rcode.calQrcode(content);
            for (int i = 0; i < codeOut.length; i++) {
                for (int j = 0; j < codeOut.length; j++) {
                    if (codeOut[j][i]) {
                        gs.fillRect(j * s + x, i * s + y, s, s);
                    }
                }
            }
            System.out.println("OK");
        } else {
            System.err.println("QRCode content bytes length = " + content.length + " not in [ 0,120 ]. ");
        }
        gs.dispose();
        bufImg.flush();
        File imgFile = new File("C:\\MESRP\\qr.jpg");
        try {
            ImageIO.write(bufImg, "jpg", imgFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
	}
}
